<!--  -->
<template>
  <div class="app-container">
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item :label="labelName">
              <el-input v-model="form.name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="上架状态">
              <el-select v-model="form.is_on_shelf" clearable placeholder="请选择">
                <el-option
                  v-for="item in isOnList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="选择省份">
              <el-select v-model="form.province_id" clearable placeholder="请选择">
                <el-option
                  v-for="item in provinceList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button type="primary" @click="handleReset">重置</el-button>
              <el-button type="primary" @click="toRoutes('addEntrance','','add')">添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <!-- :label="cate_id == 1 || cate_id == 4?'序号':'入口位置'" -->
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="100"
          align="center"
          :index="table_index"
        />
        <!-- <el-table-column
          :prop="item "
          :label="titleThead[item]"
          v-for="(item,index) of tHead"
          :key="index"
          class="df_center"
        >-->
        <el-table-column
          :prop="item.val"
          :label="item.lab"
          v-for="(item,index) of tHead"
          :key="index"
          align="center"
        >
          <template slot-scope="scope">
            <div v-if="item.val == 'image'">
              <img style="height:60px;width:60px" :src="scope.row.image | imgUrlFn" alt />
            </div>
            <div v-if="item.val == 'is_on_shelf'">
              <el-switch
                v-model="scope.row.is_on_shelf"
                class="switchStyle"
                active-color="#13ce66"
                inactive-color="#eee"
                :active-value="0"
                :inactive-value="1"
                @click.native="changeStatus(scope.row.id,scope.row.is_on_shelf)"
              />
            </div>
            <div v-if="item.val != 'image' && item.val != 'is_on_shelf'">{{scope.row[item.val]}}</div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="toRoutes('addEntrance',scope.row.id,'edit')"
            >编辑</el-button>
            <el-button type="text" size="small" @click="handleDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { appIndex, adsOnshelf, adsDel } from "@/api/appList";
import newUpload from "@/components/UploadImg/shopImgUp";
export default {
  components: { newUpload },
  filters: {
    imgUrlFn: function (opt) {
      return "https://img.yuepinyuemei.com/" + opt;
    },
  },
  // name: "MainEntrance",
  props: {
    cate_id: {
      cate_id: Number,
      // default: "",
    },
  },
  data() {
    return {
      form: {
        name: "",
        province_id: "",
        is_on_shelf: "",
        cate_id: this.cate_id,
        page: 1,
        pageSize: 10,
      },
      labelName:
        this.cate_id == 1
          ? "轮播图名称"
          : this.cate_id == 2
          ? "主要入口名称"
          : this.cate_id == 3
          ? "营销矩阵名称"
          : this.cate_id == 4
          ? "弹窗名称"
          : "",
      provinceList: [],
      isOnList: [],
      tableLoading: false,
      tableData: [],
      tHead: [],
      titleThead: {},
      page_totals: 0,
      page_sizes: [10, 30, 50, 100],
    };
  },

  mounted() {
    // console.log("cate_id", this.form);
    this.getList(this.form);
  },

  methods: {
    // 编号
    table_index(index) {
      return (this.form.page - 1) * this.form.pageSize + index + 1;
    },
    // 跳转
    toRoutes(name, id, type, cate_id) {
      const route = {
        path: name,
        query: { id: id, type: type, cate_id: this.cate_id },
      };
      this.$router.push(route);
    },
    async getList(opt) {
      let res = await appIndex(opt);
      if (res.data.status == 0) {
        let resData = res.data.data;
        let arr = [];
        for (let i in resData.t_head) {
          let obj = {
            val: i,
            lab: resData.t_head[i],
          };
          // console.log("i", i);
          // console.log("val", resData.t_head[i]);
          arr.push(obj);
        }
        this.tHead = arr;
        // this.tHead = Object.keys(resData.t_head);
        // this.titleThead = resData.t_head;
        // this.tHead = Object.keys(resData.t_head);
        // console.log("tHead", this.tHead);
        this.tableData = resData.list;
        this.page_totals = resData.total;
        this.provinceList = resData.provinceArray;
        this.isOnList = resData.onShelfArray;
      } else {
        this.$message.error(res.data.message);
      }
    },
    // 查询
    handleSearch() {
      this.form.page = 1;
      this.getList(this.form);
    },
    // 删除
    async handleDel(id) {
      let data = { id };
      const confirmResult = await this.$confirm(
        "此操作将会删除该数据, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => {
        this.$message.info("已取消");
      });
      if (confirmResult !== "confirm") {
        this.$message.info("已取消");
      } else {
        let { data: res } = await adsDel(data);
        if (res.status == 0) {
          this.$message.success("删除成功");
          this.getList(this.form);
        } else {
          this.$message.error(res.message);
        }
      }
    },
    // 上下架与状态修改
    async changeStatus(id, on_shelf) {
      const data = { id, on_shelf };
      let { data: res } = await adsOnshelf(data);
      // console.log("res", res);
      if (res.status == 0) {
        this.$message.success("修改成功");
      } else {
        this.$message.error(res.message);
      }
    },
    // 重置
    handleReset() {
      this.form.name = "";
      this.form.is_on_shelf = "";
      this.form.province_id = "";
      this.getList(this.form);
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
  },
};
</script>
<style  scoped>
.pagesCon {
  text-align: right;
  margin-top: 20px;
}
.df_center {
  display: flex;
}
</style>
